package beans;


import beans.AutoCompleteBean;
import com.icesoft.faces.component.selectinputtext.SelectInputText;
import core.Book;
import core.Course;
import core.Program;
import core.School;
import db.JDBC.core.BookJdbcDAOCore;
import db.JDBC.core.CourseJdbcDAOCore;
import db.JDBC.core.ProgramJdbcDAOCore;
import db.JDBC.core.SchoolJdbcDAOCore;
import db.core.interfaces.BookDAOCore;
import db.core.interfaces.CourseDAOCore;
import db.core.interfaces.ProgramDAOCore;
import db.core.interfaces.SchoolDAOCore;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Anton
 */

public class MenuSearchBean extends AutoCompleteBean
{
    BookDAOCore bookDAO = BookJdbcDAOCore.getInstance();
    CourseDAOCore courseDAO = CourseJdbcDAOCore.getInstance();
    ProgramDAOCore programDAO = ProgramJdbcDAOCore.getInstance();
    SchoolDAOCore schoolDAO = SchoolJdbcDAOCore.getInstance();

    private Collection<Book> books;
    private Collection<Course> courses;
    private Collection<Program> programs;
    private Collection<School> schools;

    /** Creates a new instance of MenuSearchBean */
    public MenuSearchBean()
    {
        super();
        books = new ArrayList<Book>();
        courses = new ArrayList<Course>();
        programs = new ArrayList<Program>();
        schools = new ArrayList<School>();
    }
    
    public void search(ActionEvent e)
    {
        if(searchString == null)
            return;

        books.clear();
        courses.clear();
        programs.clear();
        schools.clear();
        
        for(Book b : bookDAO.searchForObjectBooks(searchString))
            books.add(b);
        for(Course c : courseDAO.searchForObjectCourses(searchString))
            courses.add(c);
        for(Program p : programDAO.searchForObjectPrograms(searchString))
            programs.add(p);
        for(School s : schoolDAO.searchForObjectSchools(searchString))
            schools.add(s);
        try{
            FacesContext.getCurrentInstance().getExternalContext().redirect("searchLayout.jspx");
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }


    public void selectInputValueChanged(ValueChangeEvent event)
    {
        if (event.getComponent() instanceof SelectInputText)
        {
            SelectInputText autoComplete = (SelectInputText) event.getComponent();
            String newWord = (String) event.getNewValue();
            matchPossibilities = generateMatches(newWord, 10);

            if (autoComplete.getSelectedItem() != null)
                searchString = (String) autoComplete.getSelectedItem().getValue();
        }
    }
    public List<SelectItem> generateMatches(String searchWord, int maxMatches)
    {
        ArrayList<String> matchList = new ArrayList();
        List<SelectItem> returnCollection = new ArrayList<SelectItem>();

        if ((searchWord == null) || (searchWord.trim().length() == 0))
            return returnCollection;        

        for(String s : bookDAO.searchForStringBooks(searchWord, maxMatches))
            matchList.add(s);
        for(String s : courseDAO.searchForStringCourses(searchWord, maxMatches))
            matchList.add(s);
        for(String s : programDAO.searchForStringPrograms(searchWord, maxMatches))
            matchList.add(s);
        for(String s : schoolDAO.searchForStringSchools(searchWord, maxMatches))
            matchList.add(s);

        Collections.sort(matchList);

        if(matchList.size() < maxMatches)
            maxMatches = matchList.size();

        for(String s : matchList.subList(0, maxMatches))
            returnCollection.add(new SelectItem(s));
        return returnCollection;
    }


    public Collection<Book> getBooks()
    {
        return books;
    }
    public Collection<Course> getCourses()
    {
        return courses;
    }
    public Collection<Program> getPrograms()
    {
        return programs;
    }
    public Collection<School> getSchools()
    {
        return schools;
    }

}
